#ifndef UDPSERVER_H
#define UDPSERVER_H

#include <QObject>
#include <QVector>
#include <QMap>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QUdpSocket>
#include "log.h"


struct PortInstance
{
    QUdpSocket* udpSocket = nullptr;
    QString type;
    int dbIndex;           // 数据库记录索引
    QString targetHost;    // 目标主机IP
    quint16 targetPort;    // 目标端口
    quint16 localPort;     // 本地端口
};

class udpserver : public QObject
{
    Q_OBJECT
public:
    explicit udpserver(QObject *parent = nullptr);
    ~udpserver();
    QMap<int, QVector<QVariant>> readDatabase();
    void setupUdpService(int dbIndex, QVector<QVariant> config);
    void onSerialrDataReceived(int dbIndex, const QByteArray& data);

    // 获取数据库索引到端口索引的映射
    QMap<int, int> getDbIndexToPortIndexMap() const { return dbIndexToPortIndex; }

    // 检查数据库是否打开
    bool isDatabaseOpen() const;

<<<<<<< HEAD
    // 发送数据接口
    void sendData(int dbIndex, const QByteArray& data);
    void sendData(const QString& targetIp, quint16 targetPort, const QByteArray& data);
    void sendData(const QString& targetIp, quint16 targetPort, quint16 sourcePort, const QByteArray& data);
=======
private:
    QMap<int, QVector<QVariant>> serviceData; // 数据库索引 -> 行数据
    QVector<PortInstance> ports; // 端口实例列表
    QMap<int, int> dbIndexToPortIndex; // 数据库索引 -> 端口列表索引
    Log* m_logger;
>>>>>>> bd87e5561ea1c8d412e8d2a4f55b15f5140bd2a5

private:
    QMap<int, QVector<QVariant>> serviceConfig; // 数据库配置数据
    QVector<PortInstance> ports;                // 端口实例列表
    QMap<int, int> dbIndexToPortIndex;          // 数据库索引 -> 端口列表索引
    QMap<quint16, int> localPortToIndex;        // 本地端口 -> 端口列表索引
};

#endif // UDPSERVER_H
